import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, Platform} from 'ionic-angular';
import {AppConfig} from "../../../app/appConfig";
import {HttpClient} from "@angular/common/http";
import {CommonService} from "../../../app/common.service";
import {AppUrl, TIME_OUT} from "../../../app/app.general";

/**
 * Generated class for the AliPaySuccessPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'ali-pay-success',
  templateUrl: 'ali-pay-success.html',
})
export class AliPaySuccessPage {
  private param: string;
  private orderNo: string;
  private orderDetails = {
    orderDetails: [],
    noPrintDetails: [],
    orderNo: '',
    orderDate: '',
    remark: '',
    sumPrice: '',
    tId: 0
  };
  private loaded = false;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private appConfig: AppConfig,
              private httpService: HttpClient,
              private cs: CommonService,
              private platform: Platform) {
    this.param = this.platform.url();
    let params: any[];
    params = this.param.split('&');
    for (let p of params) {
      if (p.startsWith('out_trade_no')) {
        this.orderNo = p.split('=')[1];
      }
    }
    this.appConfig.presentLoading("确认支付中");
    this.httpService.post(AppUrl.food.confirmOrder, {
      orderNo: this.orderNo,
      from: '99'
    }).timeout(TIME_OUT).subscribe(data => {
        let temp: any = data;
        if (temp.code == 200) {
          this.httpService.post(AppUrl.food.getOrderDetail, this.orderNo).timeout(TIME_OUT).subscribe(data => {
            this.appConfig.loader.dismiss();
            let temp: any = data;
            if (temp.code == 200) {
              this.orderDetails = temp.data;
              this.loaded = true;
            }
            else {
              this.appConfig.presentToast(temp.msg);
            }
          }, e => {
            this.appConfig.loader.dismiss();
            this.cs.httpError(e);
          })
        }
      }, e => {
        this.appConfig.loader.dismiss();
        this.cs.httpError(e);
      }
    );
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad AliPaySuccessPage');
  }

}
